package com.leyou.page.service;

import com.leyou.item.api.BrandApi;
import com.leyou.item.api.CategoryApi;
import com.leyou.item.api.GoodsApi;
import com.leyou.item.api.SpecificationApi;
import com.leyou.item.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: 商品详情业务
 * @author 朱正磊
 * @date 2021-04-12 12:21:07
 */
@Slf4j
@Service
public class PageService {

    @Resource
    private GoodsApi goodsApi;

    @Resource
    private CategoryApi categoryApi;

    @Resource
    private BrandApi brandApi;

    @Resource
    private SpecificationApi specificationApi;

    @Resource
    private TemplateEngine templateEngine;

    /**
     * description: 查询模型数据
     * @author 朱正磊
     * @date 2021-04-12 12:23:30
     * @param spuId spu的id
     * @return 返回模型数据
     */
    public Map<String, Object> loadModel(Long spuId) {
        Map<String, Object> attributes = new HashMap<>(16);
        // 查询spu包括skus以及spuDetail
        Spu spu = goodsApi.querySpuById(spuId);
        attributes.put("spu", spu);
        // 查询spu详情
        SpuDetail detail = spu.getSpuDetail();
        attributes.put("detail", detail);
        // 查询skus
        List<Sku> skus = spu.getSkus();
        attributes.put("skus", skus);
        // 查询品牌
        Long brandId = spu.getBrandId();
        Brand brand = brandApi.queryById(brandId);
        attributes.put("brand", brand);
        // 查询分类
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<Category> categories = categoryApi.queryByIds(cids);
        attributes.put("categories", categories);
        // 查询规格参数组以及组内规格参数
        List<SpecGroup> specs = specificationApi.queryGroupsByCid(spu.getCid3());
        attributes.put("specs", specs);
        return attributes;
    }

    /**
     * description: thymeleaf页面静态化
     * @author 朱正磊
     * @date 2021-04-12 14:52:51
     * @param spuId spu的id
     */
    public void createHtml(Long spuId) {
        // 上下文
        Context context = new Context();
        context.setVariables(this.loadModel(spuId));

        // 指定静态页文件
        File dest = new File("D:\\study_video\\JAVA-springcloud\\upload_file_dir", spuId + ".html");
        // 如果静态页已经存在则需要先删除
        this.deleteFile(dest);

        try(PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
            // 生成HTML
            templateEngine.process("item", context, writer);
        } catch (Exception e) {
            log.error("【静态页服务】生成静态页异常：", e);
        }
    }

    /**
     * description: 删除静态页
     * @author 朱正磊
     * @date 2021-04-14 14:10:15
     * @param spuId spu的id
     */
    public void deleteHtml(Long spuId) {
        // 指定静态页文件
        File dest = new File("D:\\study_video\\JAVA-springcloud\\upload_file_dir", spuId + ".html");
        // 如果静态页已经存在则需要先删除
        this.deleteFile(dest);
    }

    /**
     * description: 如果静态页已经存在则需要先删除
     * @author 朱正磊
     * @date 2021-04-14 14:11:28
     * @param dest 待删除的静态页
     */
    private void deleteFile(File dest) {
        if (dest.exists()) {
            boolean flag = dest.delete();
            if (!flag) {
                throw new RuntimeException("【静态页微服务】删除已经过时的静态页失败！");
            }
        }
    }

}
